package com.minzheng.blog.controller;

import cn.hutool.db.sql.Condition;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.minzheng.blog.dao.ArticleDao;
import com.minzheng.blog.dao.PhotoAlbumDao;
import com.minzheng.blog.dao.PhotoDao;
import com.minzheng.blog.dto.ArticleDTO;
import com.minzheng.blog.dto.ArticleHomeDTO;
import com.minzheng.blog.dto.PhotoAlbumBackDTO;
import com.minzheng.blog.dto.PhotoDTO;
import com.minzheng.blog.service.ArticleService;
import com.minzheng.blog.service.PhotoService;
import com.minzheng.blog.util.MarkdownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author wangdiyi
 * @date 2023/4/17 21:04:13
 */
@Controller
public class NewsController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private PhotoAlbumDao photoAlbumDao;
    @Autowired
    private PhotoService photoService;
    /*
     * 查看前台竞赛新闻列表
     *
     * */
    @GetMapping("/front/news_list")
    public String NewsList(Model model,
                           @RequestParam(value="pageNum", defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
     //   Page page =PageHelper.startPage(pageNum, pageSize);
        if(pageNum<=0)
            pageNum=0;
        PageMethod.startPage(pageNum, 5);
        List<ArticleHomeDTO> newslist=articleService.Newslist();
        // 将分页结果封装到PageInfo对象中
        PageInfo<ArticleHomeDTO> pageInfo = new PageInfo<>(newslist);
        model.addAttribute("pageInfo", pageInfo);
        return "newslist";
    }
    /*
    *
    * 查看竞赛通知列表
    * */
    @GetMapping("/front/notices_list")
    public String NoticesList(Model model,
                           @RequestParam(value="pageNum", defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        //   Page page =PageHelper.startPage(pageNum, pageSize);
        if(pageNum<=0)
            pageNum=0;
        PageMethod.startPage(pageNum, 5);
        List<ArticleHomeDTO> noticeslist=articleService.Noticeslist();
        // 将分页结果封装到PageInfo对象中
        PageInfo<ArticleHomeDTO> pageInfo = new PageInfo<>(noticeslist);
        model.addAttribute("pageInfo", pageInfo);
        return "noticeslist";
    }
    /*
     *
     * 查看竞赛题解列表
     * */
    @GetMapping("/front/answers_list")
    public String AnswersList(Model model,
                           @RequestParam(value="pageNum", defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        //   Page page =PageHelper.startPage(pageNum, pageSize);
        if(pageNum<=0)
            pageNum=0;
        PageMethod.startPage(pageNum, 5);
        List<ArticleHomeDTO> answerslist=articleService.Answerslist();
        // 将分页结果封装到PageInfo对象中
        PageInfo<ArticleHomeDTO> pageInfo = new PageInfo<>(answerslist);
        model.addAttribute("pageInfo", pageInfo);
        return "answerslist";
    }

    /*
     *
     * 查看竞赛现场相册列表
     * */
    @GetMapping("/front/gallerys_list")
    public String GallerysList(Model model,
                              @RequestParam(value="pageNum", defaultValue = "1") int pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "6") int pageSize){
        //   Page page =PageHelper.startPage(pageNum, pageSize);
        if(pageNum<=0)
            pageNum=0;
        PageMethod.startPage(pageNum, pageSize);
        List<PhotoAlbumBackDTO> galleryslist=photoAlbumDao.listPhotoAlbumFronts();
        // 将分页结果封装到PageInfo对象中
        PageInfo<PhotoAlbumBackDTO> pageInfo = new PageInfo<>(galleryslist);
        model.addAttribute("pageInfo", pageInfo);
        return "gallery";
    }

    /*
     *
     * 查看名人堂列表
     * */
    @GetMapping("/front/peoples_list")
    public String PeoplesList(Model model,
                           @RequestParam(value="pageNum", defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "6") int pageSize){
        //   Page page =PageHelper.startPage(pageNum, pageSize);
        if(pageNum<=0)
            pageNum=0;
        PageMethod.startPage(pageNum, pageSize);
        List<ArticleHomeDTO> peopleslist=articleService.Peopleslist();
        // 将分页结果封装到PageInfo对象中
        PageInfo<ArticleHomeDTO> pageInfo = new PageInfo<>(peopleslist);
        model.addAttribute("pageInfo", pageInfo);
        return "peopleslist";
    }

    @GetMapping("/blogs/{articleId}")
    public String getNewsById(@PathVariable("articleId") Integer articleId, Model model) {
        ArticleDTO articleDTO=articleService.getArticleById(articleId);
        articleDTO.setViewsCount(articleDTO.getViewsCount()+1);

        articleDTO.setArticleContent(MarkdownUtil.markdownExtToHtml(articleDTO.getArticleContent()));

        model.addAttribute("news",articleDTO);
        return "blog-details";
    }

    @GetMapping("/blog/{articleId}")
    public String getTestById(@PathVariable("articleId") Integer articleId, Model model) {
        ArticleDTO articleDTO=articleService.getArticleById(articleId);
        articleDTO.setViewsCount(articleDTO.getViewsCount()+1);

        articleDTO.setArticleContent(MarkdownUtil.markdownExtToHtml(articleDTO.getArticleContent()));

        model.addAttribute("news",articleDTO);
        return "mdtest";
    }

    @GetMapping("/photos_list/{albumId}")
    public String getPhotoById(@PathVariable("albumId") Integer albumId, Model model) {
        PhotoDTO photoDTO=photoService.listPhotosByAlbumId(albumId);
        System.out.println(photoDTO);
        model.addAttribute("photoslist",photoDTO.getPhotoList());
        System.out.println(photoDTO.getPhotoList());
        model.addAttribute("albumName",photoDTO.getPhotoAlbumName());
        return "photo";
    }

    @GetMapping("/code")
    public String showCodePage(Model model) {

        // 将lang、str和codeIndex添加到modelAndView中
        String lang = "java";
        String str = "public static void main(String[] args) {\n  System.out.println(\"Hello World!\");\n}";
        int codeIndex = 1;
        model.addAttribute("lang", lang);
        model.addAttribute("str", str);
        model.addAttribute("codeIndex", codeIndex);

        return "mdtest";
    }
}
